package mc.support.util;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.*;

/**
 * Created with IntelliJ IDEA. <br/>
 * User: francis    <br/>
 * Date: 13-11-20    <br/>
 * Time: 19:13  <br/>
 */
public final class SerializeUtil {

    private static Logger log = LoggerFactory.getLogger( SerializeUtil.class );

    private SerializeUtil(){}

    public static byte[] serializeToByteArray(Object o) {
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        ObjectOutput out = null;
        byte[] bs = null;
        try {
            out = new ObjectOutputStream(bos);
            out.writeObject( o );
            bs = bos.toByteArray();
        } catch (IOException e) {
            log.error( "serialize " + o.getClass().getName() + " to byte array failed :"
            + e.getMessage() );
        } finally {
            if (out != null) {
                try {
                    out.close();
                } catch (IOException e) {
                }
            }
            if ( null != bos ) {
                try {
                    bos.close();
                } catch (IOException e) {
                }
            }
        }

        return bs;
    }

    public static Object deserializeToObject(byte[] bs) {
        ByteArrayInputStream bis = new ByteArrayInputStream( bs );
        ObjectInput in = null;
        Object o = null;
        try {
            in = new ObjectInputStream( bis );
            o = in.readObject();
        } catch (ClassNotFoundException e) {
            logDeserializeException( e );
        } catch (IOException e) {
            logDeserializeException( e );
        } finally {
            if (in != null) {
                try {
                    in.close();
                } catch (IOException e) {
                }
            }
            if ( null != bis ) {
                try {
                    bis.close();
                } catch (IOException e) {
                }
            }
        }

        return o;
    }

    public static void serializeToFile(Object obj, String filePath) throws IOException {
        File f = new File(filePath);
        if (!f.exists()) {
            f.createNewFile();
        }
        FileOutputStream fos = new FileOutputStream(f);
        ObjectOutputStream oos =  new ObjectOutputStream(fos);
        oos.writeObject(obj);
        oos.flush();
        oos.close();
    }

    public static Object deserializeFromFile(String filePath) throws IOException, ClassNotFoundException {
        File file = new File(filePath);
        return deserializeFromFile(file);
    }

    public static Object deserializeFromFile(File f) throws IOException, ClassNotFoundException {
        Object cachedObj;
        if (!f.exists()) {
            return null;
        }

        FileInputStream fis = new FileInputStream(f);
        ObjectInputStream ois = new ObjectInputStream(fis);
        cachedObj = ois.readObject();
        ois.close();

        return cachedObj;
    }

    private static void logDeserializeException(Exception e) {
        log.error( "deserialize byte array to object failed, due to: " + e.getMessage() );
    }

}
